﻿package view.mainUI.mainBottom.controller
{
    import com.core.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import view.mainUI.mainBottom.ui.*;
    import view.uilt.*;

    public class MainExpTweenControl extends Object
    {
        private var mUI:MainBottom;
        private var mFirst:Boolean = true;
        private var mCurLevel:int = 0;
        private var mToLevel:int = 0;
        private var mToWidth:int = 0;
        private var mUpdateing:Boolean = false;
        private const EXP_WIDTH:int = 444;
        private var _exptimeID:uint = 0;

        public function MainExpTweenControl(param1:MainBottom)
        {
            var _loc_2:Shape = null;
            this.mUI = param1;
            _loc_2 = new Shape();
            _loc_2.graphics.beginFill(0, 0);
            _loc_2.graphics.drawRect(0, 0, 444, 11);
            _loc_2.graphics.endFill();
            _loc_2.x = 178;
            _loc_2.y = 116;
            this.ui.exppoint.mask = _loc_2;
            this.ui.exppoint.blendMode = BlendMode.ADD;
            this.ui.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            var _loc_2:* = new Point(this.ui.expProgress.x + this.ui.expProgress.width, this.ui.expProgress.y + this.ui.expProgress.height / 2);
            ItemUilt.ExpPoint = this.ui.expProgress.parent.localToGlobal(_loc_2);
            return;
        }// end function

        public function updateExp() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            ModelProxy.change("lianzhiexp", "UpdataEXP", null);
            var _loc_1:* = UserData.getUserData("level");
            if (this.mFirst)
            {
                this.mFirst = false;
                this.mCurLevel = _loc_1;
                this.directUpdate();
            }
            else
            {
                if (this.mUpdateing)
                {
                    TweenLite.killTweensOf(this.ui.expLight);
                    TweenLite.killTweensOf(this.ui.expProgress);
                    this.ui.expProgress.width = this.mToWidth;
                    this.ui.expLight.width = this.mToWidth;
                    this.mCurLevel = this.mToLevel;
                }
                _loc_2 = ConfigDictionary.data.level[_loc_1];
                _loc_3 = UserData.getUserData("exp");
                this.mToLevel = _loc_1;
                this.mToWidth = int(Math.min(_loc_3 / _loc_2, 1) * this.EXP_WIDTH);
                if (this.mToLevel < this.mCurLevel || this.mToLevel == this.mCurLevel && this.mToWidth < this.ui.expProgress.width)
                {
                    this.mCurLevel = _loc_1;
                    this.directUpdate();
                }
                else
                {
                    this.startTween();
                }
            }
            return;
        }// end function

        private function directUpdate() : void
        {
            var _loc_1:* = UserData.getUserData("level");
            var _loc_2:* = ConfigDictionary.data.level[_loc_1];
            var _loc_3:* = UserData.getUserData("exp");
            this.ui.expProgress.width = int(Math.min(_loc_3 / _loc_2, 1) * this.EXP_WIDTH);
            this.ui.expLight.width = this.ui.expProgress.width;
            var _loc_4:* = new Point(this.ui.expProgress.x + this.ui.expProgress.width, this.ui.expProgress.y + this.ui.expProgress.height / 2);
            ItemUilt.ExpPoint = this.ui.expProgress.parent.localToGlobal(_loc_4);
            return;
        }// end function

        private function startTween() : void
        {
            this.mUpdateing = true;
            var _loc_1:* = this.mCurLevel == this.mToLevel ? (this.mToWidth) : (this.EXP_WIDTH);
            var _loc_2:* = 1 * ((_loc_1 - this.ui.expLight.width) / this.EXP_WIDTH);
            _loc_2 = _loc_2 < 0.5 ? (0.5) : (_loc_2);
            TweenLite.to(this.ui.expLight, _loc_2, {width:_loc_1, onComplete:this.expLightComplete, onCompleteParams:[_loc_1]});
            return;
        }// end function

        private function expLightComplete(param1:int) : void
        {
            var _loc_2:* = 2 * ((param1 - this.ui.expProgress.width) / this.EXP_WIDTH);
            _loc_2 = 0.04;
            if (this.mCurLevel == this.mToLevel && UserData.getUserData("level") >= 60)
            {
                this.ui.exppoint.visible = true;
                this.ui.exppoint.x = param1 + 177 - this.ui.exppoint.width;
            }
            TweenLite.to(this.ui.expProgress, _loc_2, {width:param1, onComplete:this.expProgressComplete});
            return;
        }// end function

        private function expProgressComplete() : void
        {
            var p:Point;
            if (this.mCurLevel == this.mToLevel)
            {
                this.mUpdateing = false;
                this.ui.expLight.width = this.ui.expProgress.width;
                if (UserData.getUserData("level") >= 60)
                {
                    this.ui.exppoint.visible = true;
                    this.ui.exppoint.x = this.ui.expLight.width + 177 - this.ui.exppoint.width + 5;
                    if (this._exptimeID > 0)
                    {
                        clearTimeout(this._exptimeID);
                        this._exptimeID = 0;
                    }
                    this._exptimeID = setTimeout(function () : void
            {
                if (_exptimeID > 0)
                {
                    clearTimeout(_exptimeID);
                    _exptimeID = 0;
                }
                ui.exppoint.visible = false;
                return;
            }// end function
            , 0.1);
                }
                p = new Point(this.ui.expProgress.x + this.ui.expProgress.width, this.ui.expProgress.y + this.ui.expProgress.height / 2);
                ItemUilt.ExpPoint = this.ui.expProgress.parent.localToGlobal(p);
                return;
            }
            var _loc_2:String = this;
            var _loc_3:* = this.mCurLevel + 1;
            _loc_2.mCurLevel = _loc_3;
            this.ui.expProgress.width = 0;
            var updateWidth:* = this.mCurLevel == this.mToLevel ? (this.mToWidth) : (this.EXP_WIDTH);
            var time:* = 1 * (updateWidth / this.EXP_WIDTH);
            time = time < 0.5 ? (0.5) : (time);
            this.ui.expLight.width = 0;
            TweenLite.to(this.ui.expLight, time, {width:updateWidth, onComplete:this.expLightComplete, onCompleteParams:[updateWidth]});
            return;
        }// end function

        private function get ui() : MainBottom
        {
            return this.mUI;
        }// end function

    }
}
